package 队列;

import java.util.Stack;

/**
 * @author 陈艺博
 * @date 2021-02-24 21:01
 * @description https://leetcode-cn.com/problems/implement-queue-using-stacks/
 **/
public class _232_implement_queue_using_stacks_用栈实现队列 {

    private Stack<Integer> inStack;
    private Stack<Integer> outStack;

    /**
     * Initialize your data structure here.
     */
    public _232_implement_queue_using_stacks_用栈实现队列() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    /**
     * 将元素添加到队尾
     */
    public void push(int x) {
        inStack.push(x);
    }

    /**
     * 删除队头元素并且返回其值
     */
    public int pop() {
        checkOutStack();
        return outStack.pop();
    }

    /**
     *获取队头元素
     */
    public int peek() {
        checkOutStack();
        return outStack.peek();
    }

    /**
     * 是否为空
     */
    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }

    /**
     * 检测outStack是否为空，如果为空，则将inStack中的元素全部放入outStack中
     */
    private void checkOutStack() {
        if (outStack.isEmpty()) {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
        }
    }

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
}
